package activity;

import java.nio.MappedByteBuffer;

import manager.DownLoadManager;
import holder.AppDetailButtomHolder;
import holder.AppDetailDesHolder;
import holder.AppDetailInfoHolder;
import holder.AppDetailPicHolder;
import holder.AppDetailSafeHolder;

import com.example.googlemarketdemo.R;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import protocol.DetailProtocol;
import base.LoadingPager;
import base.LoadingPager.LoadResult;
import bean.AppInfoBean;
import utils.UIUtils;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

/**
 * 详情的页面
 * 
 * @author Anson-pc
 * 
 */
public class DetailActivity extends BaseActivity {

	private String mPackageName;
	private LoadingPager mloadingPager;
	private AppInfoBean appInfoBean;
	/*-----------  获取控件 ----------*/
	@ViewInject(R.id.detail_fl_bottom)
	FrameLayout mContainerBottom;

	@ViewInject(R.id.detail_fl_des)
	FrameLayout mContainerDes;

	@ViewInject(R.id.detail_fl_info)
	FrameLayout mContainerInfo;

	@ViewInject(R.id.detail_fl_pic)
	FrameLayout mContainerPic;

	@ViewInject(R.id.detail_fl_safe)
	FrameLayout mContainerSafe;
	private AppDetailButtomHolder appDetailButtomHolder;

	/*--------- end ----------*/

/*	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		init();
		initView();
		initData();
		initListener();

		// setContentView(tv);//1.加载中的页面2.错误3.成功4.空
	}*/

	
	@Override
	protected void initListener() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void initData() {
		// 触发加载数据
		mloadingPager.tiggerloadData();
	}

	@Override
	protected void initView() {
		// 加载的页面
		mloadingPager = new LoadingPager(UIUtils.getContext()) {

			/**
			 * 真正在子线程中执行加载数据
			 */
			@Override
			protected LoadResult initData() {
				// TODO Auto-generated method stub
				return DetailActivity.this.loadData();
			}

			@Override
			protected View initSuccessView() {
				// TODO Auto-generated method stub
				return DetailActivity.this.initSuccessView();
			}

		};

		setContentView(mloadingPager);// 1.加载中的页面2.错误3.成功4.空
	}

	/**
	 * 创建成功视图的方法
	 * 
	 * @return
	 */
	protected View initSuccessView() {
		View view = View.inflate(UIUtils.getContext(), R.layout.item_detail,
				null);
		// 获取控件
		ViewUtils.inject(this, view);
		// 接收holde提供的视图

		// 应用的信息部分
		AppDetailInfoHolder appDetailInfoHolder = new AppDetailInfoHolder();
		mContainerInfo.addView(appDetailInfoHolder.mHolderView);
		appDetailInfoHolder.setDataAndRefreshHolderView(appInfoBean);
		// 应用的下载部分

		appDetailButtomHolder = new AppDetailButtomHolder();
		mContainerBottom.addView(appDetailButtomHolder.mHolderView);
		appDetailButtomHolder.setDataAndRefreshHolderView(appInfoBean);
		// 应用的安全部分
		AppDetailSafeHolder appDetailSafeHolder = new AppDetailSafeHolder();
		mContainerSafe.addView(appDetailSafeHolder.mHolderView);
		appDetailSafeHolder.setDataAndRefreshHolderView(appInfoBean);
		// 应用的截图部分
		AppDetailPicHolder appDetailPicHolder = new AppDetailPicHolder();
		mContainerPic.addView(appDetailPicHolder.mHolderView);
		appDetailPicHolder.setDataAndRefreshHolderView(appInfoBean);
		// 应用的描述部分
		AppDetailDesHolder appDetailDesHolder = new AppDetailDesHolder();
		mContainerDes.addView(appDetailDesHolder.mHolderView);
		appDetailDesHolder.setDataAndRefreshHolderView(appInfoBean);

		// 添加观察者到集合中
		DownLoadManager.getInstance().addObserver(appDetailButtomHolder);
		return view;
	}

	protected LoadResult loadData() {
		// 在子线程中加载数据
		DetailProtocol protocol = new DetailProtocol(mPackageName);

		try {
			appInfoBean = protocol.loadData(0);

			// 判断数据是否为空
			if (appInfoBean != null) {
				return LoadResult.SUCCESS;
			} else {
				return LoadResult.EMPTY;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return LoadResult.ERROR;
		}

	}

	@Override
	protected void init() {
		mPackageName = getIntent().getStringExtra("packageName");
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onResume() {
		// 加入观察者
		if (appDetailButtomHolder != null) {
			
			appDetailButtomHolder.addObserverAndNotify();

//			DownLoadManager.getInstance().addObserver(appDetailButtomHolder);
			
			//手动的发布一下 
//			DownLoadManager.getInstance().addObserver(o)
		}
		super.onResume();
	}

	@Override
	protected void onPause() {
		// 移除观察者
		if (appDetailButtomHolder != null) {

			DownLoadManager.getInstance().deleteObserver(appDetailButtomHolder);
		}
		super.onPause();
	}
}
